<?php
namespace Home\Controller;

/**
 * 订单表
 * 1=>待审核 2=>审核未通过 3=>审核通过 4=>上画待验收 5=>下画待验收 6=>订单完成
 * Class OrderController
 * @package Home\Controller
 */
class OrderController extends BaseController
{
    public function __construct()
    {
    }

    /**
     * 创建活动的时候初始化订单
     * @param $activity_id
     */
    public function InitOrder($activity_id)
    {
        $activity = M('activity')->where(['id' => $activity_id])->find();
        $imgs = M('activity_image')->where(['id' => ['in', $activity['img_ids']]])->select();

        if ($activity) {
            $stores = explode(',', $activity['store_ids']);
            foreach ($stores as $v) {
                //查询门店活动图片
                $imgId = '';
                foreach ($imgs as $k1 => $v1) {
                    if (strpos(',' . $v1['store_ids'] . ',', ',' . $v . ',') !== false) {
                        $imgId .= $v1['id'] . ',';
                    }
                }

                $data = [
                    'order_no' => buildTradeNo(),
                    'activity_id' => $activity['id'],
                    'store_id' => $v,
                    'activity_image_id' => substr($imgId, 0, -1) ? substr($imgId, 0, -1) : '',
                    'order_status' => 0,
                    'audti_user_id' => $activity['audit_user_id'],
                    'add_time' => time(),
                ];

                $insertId = M('order')->add($data);
                //初始化订单详情表
                /*
                M('order_detail')->add([
                    'order_id' => $insertId,
                    'order_no' => $data['order_no'],
                    'add_time' => time()
                ]);*/
            }
        }
    }

    /**
     * 订单详情
     */
    public function detailByOrderNo()
    {
        $order_no = I('order_no');
        $order = M('order')->where(['order_no' => $order_no])->find();
        if (!$order) {
            $this->ajaxReturn(['status' => false, 'code' => 1001, 'msg' => '订单号异常', 'data' => []]);
        }

        $activity_info = M('activity')->where(['id' => $order['activity_id']])->find();
        $store_info = M('store')->where(['id' => $order['store_id']])->find();
        $audit_info = M('audit')->where(['id' => $store_info['audit_user_id']])->find();
        $ads_info = M('order_ads a')
            ->field('a.id,a.ads_name,a.ads_id,a.width,a.height,a.filepath,a.ads_type,a.materiel_id,a.activity_image_id,a.is_upper_air,a.num,a.remark')
            ->where(['a.order_no' => $order_no])->select();

        foreach ($ads_info as $k => $v) {
            $ads_info[$k]['detail'] = M("order_ads_detail b")
                ->field('b.id,b.ads_name,b.ads_detail_id,b.ads_type,b.x_axis,b.y_axis,b.width,b.height,b.remark,b.materiel_id,b.materiel_name,b.activity_image_id,b.activity_image_url,b.activity_image_name')
                ->where(['order_ads_id' => $v['id']])->select();

            if ($v['ads_type'] == 1) {//自定义广告
                $ads_info[$k]['filepath'] = C('DOMAIN') . '/' . $v['filepath'];
            }
        }

        foreach ($ads_info as $k => $v) {
            if ($v['ads_type'] == 2) {
                $materiel = M('materiel')->where(['id' => $v['materiel_id']])->field('name')->find();
                $activity_image = M('activity_image')->where(['id' => $v['activity_image_id']])->field('image_url,image_name')->find();
                $ads_info[$k]['materiel_name'] = $materiel['name'];
                $ads_info[$k]['activity_image_url'] = $activity_image['image_url'];
                $ads_info[$k]['activity_image_name'] = $activity_image['image_name'];
                $ads_info[$k]['remark'] = $v['remark'];
            }
        }

        $data = [
            'activity_name' => $activity_info['name'],  //活动名称
            'activity_id' => $activity_info['id'],  //活动id
            'order_no' => $order_no,   //订单号
            'order_status' => $order['order_status'], //订单状态
            'order_status_msg' => $this->statusMsg()[$order['order_status']], //订单状态
            'store_id' => $order['store_id'],
            'store_name' => $store_info['store_name'], //门店名字
            'store_manager' => $store_info['manager_name'], //店长名字
            'store_mobiel' => $store_info['mobile'], //店长电话
            'store_address' => $store_info['address'], //上画地址
            'audit_name' => $audit_info['name'], //审核员名字
            'audit_mobile' => $audit_info['mobile'], //审核员电话
            'up_time' => $order['up_time'], //上画时间
            'down_time' => $order['down_time'], //下画时间
            'audit_result' => $order['audit_result'], //审核不通过原因
            'is_down' => $order['is_down'], //是否下画
            'is_down_msg' => $order['is_down'] == 0 ? '否' : '是',
            'ads_info' => $ads_info,
            'upper_fee' => $order['upper_fee'],
            'parter_fee' => $order['parter_fee'],
        ];

        $this->ajaxReturn(['status' => true, 'code' => 1000, 'msg' => '订单详情查询成功', 'data' => $data]);
    }

    /**
     * 订单状态
     */
    public function orderStatusList()
    {
        $order_no = I('order_no');
        $order = M('order')->where(['order_no' => $order_no])->find();
        if (!$order) {
            $this->ajaxReturn(['status' => false, 'code' => 1001, 'msg' => '订单号异常', 'data' => []]);
        }

        $order_status_log = M('order_status_log')
            ->where(['order_no' => $order_no])
            ->field('name,add_time,remark,user_name')
            ->select();

        $this->ajaxReturn(['status' => true, 'code' => 1000, 'msg' => '订单状态列表', 'data' => $order_status_log]);
    }
}